<template>
    <div class="reset-wrap">
        <Row type="flex" justify="center" align="middle" @keydown.enter.native="submitReset" style="height: 100%">
            <div class="container">
                <Col class="left">
                    <!-- <img src="../assets/title.png"/> -->
                </Col>
                <Col class="content right">
                    <div class="content-container">
                        <!--          <Header />-->
                        <div class="header">
                            <p>找回密码</p>
                        </div>
                        <Row class="reset">
                            <Form ref="resetForm" :model="form" :rules="rules" class="form">
                                <FormItem prop="mobile" :error="errorMobile">
                                    <Input v-model="form.mobile" size="large" clearable placeholder="请输入手机号">
                                        <template #prefix>
                                            <Icon type="md-phone-portrait" />
                                        </template>
                                    </Input>
                                </FormItem>
                                <Row class="nihao">
                                    <FormItem prop="code" class="iopjh">
                                        <Input v-model="form.code" size="large" clearable placeholder="请输入验证码" :maxlength="10" class="input-verify">
                                            <template #prefix>
                                                <img src="@/assets/yzm.png" style="width: 22px; height: 22px" />
                                            </template>
                                        </Input>
                                    </FormItem>
                                    <!-- 获取验证码按钮 -->
                                    <CountDownButton ref="countDown" @on-click="showVaptcha" :autoCountDown="false" size="large" :loading="sending" :text="getSms" />
                                </Row>

                                <FormItem porp="password">
                                    <SetPassword size="large" @on-change="changeInputPass1" placeholder="请输入新密码" />
                                </FormItem>
                                <FormItem porp="password2">
                                    <SetPassword size="large" @on-change="changeInputPass2" placeholder="请再次输入新密码" />
                                </FormItem>
                            </Form>

                            <Button style="margin-bottom: 3vh" type="primary" size="large" :loading="loading" @click="submitReset" long>确认修改</Button>
                            <Row type="flex">
                                <a @click="$router.go(-1)" class="back">
                                    <Icon type="md-arrow-round-back" style="margin-right: 3px" />
                                    返回
                                </a>
                            </Row>
                        </Row>
                    </div>
                </Col>
            </div>

            <LangSwitch />
        </Row>
    </div>
</template>

<script>
import { vaptchaID, vaptchaOffline, sendResetSms, resetByMobile, sendResetEmail, resetByEmail, sendRetrievePassCode } from '@/api/index';
import { validateMobile, validatePassword, validateIDCard } from '@/libs/validate';
import Header from '@/views/main-components/header';
import Footer from '@/views/main-components/footer';
import LangSwitch from '@/views/main-components/lang-switch';
import CountDownButton from '@/views/my-components/xboot/count-down-button';
import SetPassword from '@/views/my-components/xboot/set-password';

export default {
    components: {
        CountDownButton,
        LangSwitch,
        SetPassword,
        Header,
        Footer
    },
    data() {
        const validateConfirmPass = (rule, value, callback) => {
            if (value.length < 6) {
                callback(new Error('密码长度不得小于6位'));
            } else if (value !== this.form.password) {
                callback(new Error('两次输入的密码不一致'));
            } else {
                callback();
            }
        };
        return {
            cityList: [],
            label: '使用手机号重置密码',
            icon: 'md-phone-portrait',
            loading: false,
            type: '0',
            form: {
                mobile: '',
                code: '',
                password1: '',
                password2: '',
                passStrength1: '',
                passStrength2: ''
            },
            sended: false,
            sending: false,
            getSms: '获取验证码',
            select: '86',
            errorMobile: '',
            errorEmail: '',
            rules: {
                mobile: [
                    {
                        required: true,
                        message: '手机号不能为空',
                        trigger: 'blur'
                    },
                    {
                        validator: validateMobile,
                        trigger: 'change'
                    }
                ]
            },
            vaptchaObject: null
        };
    },
    mounted() {
        this.initVaptcha();
    },
    methods: {
        initVaptcha() {
            let that = this;
            vaptcha({
                //配置参数
                vid: vaptchaID, // 验证单元id
                type: 'invisible', // 展现类型 隐藏式
                offline_server: vaptchaOffline // 离线验证接口地址 可选但此处不能为空
            }).then(function (vaptchaObj) {
                that.vaptchaObject = vaptchaObj;
                vaptchaObj.listen('pass', function () {
                    that.form.token = vaptchaObj.getToken();
                    // 验证成功 发送验证码
                    if (that.type == '0') {
                        that.sendSmsCode();
                    } else if (that.type == '1') {
                        that.sendEmailCode();
                    }
                });
            });
        },
        // 点击获取验证码按钮判断手机格式
        showVaptcha() {
            var reg = /^[1][3,4,5,6,7,8][0-9]{9}$/;
            if (!reg.test(this.form.mobile)) {
                this.errorMobile = '手机号格式错误';
                return;
            } else {
                this.errorMobile = '';
                this.sendSmsCode(); // 进行验证码的发送
            }
            // this.vaptchaObject.validate(); // 若没验证验证码 开始验证
        },
        sendSmsCode() {
            this.sending = true;
            this.getSms = '发送中';
            sendRetrievePassCode({ moble: this.form.mobile }).then(res => {
                this.getSms = '已获取验证码';
                this.sending = false;
                this.vaptchaObject.reset();
                if (res.success) {
                    this.$refs.fileUrlUploadFiles.setData();
                    this.$Message.success('发送短信验证码成功');
                    // 开始倒计时
                    this.$refs.countDown.startCountDown();
                }
            });
        },
        // 密码强度
        changeInputPass1(v, grade, strength) {
            this.form.passStrength1 = strength; //密码强度
            this.form.password1 = v; // 密码
        },
        changeInputPass2(v, grade, strength) {
            this.form.passStrength2 = strength; //密码强度
            this.form.password2 = v; // 密码
        },
        // 提交申请
        submitReset() {
            if (this.form.password1 === this.form.password2) {
                this.$refs.resetForm.validate(valid => {
                    if (valid) {
                        console.log('aaaaaaaaaaaaaaaa', this.form);
                        let form = {
                            mobile: this.form.mobile,
                            code: this.form.code,
                            password: this.form.password1,
                            passStrength: this.form.passStrength
                        };
                        this.loading = true;
                        resetByMobile(form).then(res => {
                            console.log('====', res);
                            this.loading = false;
                            if (res.success) {
                                this.$Message.success('申请成功！');
                            } else {
                                // this.$Message.error(res.message);
                                this.$Message.error({
                                    content: res.message,
                                    duration: 5,
                                    closable: true
                                });
                            }
                        });
                    }
                });
            } else {
                // this.$Message.error('两次输入密码不一致,请重新输入');
                this.$Message.error({
                    content: '两次输入密码不一致,请重新输入',
                    duration: 5,
                    closable: true
                });
            }
        }
    }
};
</script>

<style lang="less">
@import './proreset.less';
</style>
